//
//  MovieViewController.m
//  UI14_豆瓣电影更新版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieModel.h"
#import "MovieTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "RootCollectionViewCell.h"
@interface MovieViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, retain) NSMutableArray *arrData;
@property (nonatomic, retain) UITableView *tableview;
@property (nonatomic, retain) NSMutableArray *arrModel;

@end

@implementation MovieViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)dealloc {
    [_arrData release];
    [_tableview release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"电影";
    
    UIImage *img5 = [UIImage imageNamed:@"btn_nav_collection@2x.png"];
    img5 = [img5 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:img5 style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    item.tag = 1000;
    self.navigationItem.rightBarButtonItem = item;
    [item release];
    
    // 数据解析
    NSString *Path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    self.arrData = [NSMutableArray arrayWithContentsOfFile:Path];
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49) style:UITableViewStylePlain];
    [self.view addSubview:_tableview];
    [_tableview release];
    // 设置代理人
    _tableview.delegate = self;
    _tableview.dataSource = self;
    
    // 注册重用池
    [_tableview registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.arrModel = [NSMutableArray array];
    for (NSDictionary *dic in _arrData) {
        MovieModel *model = [MovieModel movieModel];
        [model setValuesForKeysWithDictionary:dic];
        [_arrModel addObject:model];
    }
    

    
    
}
// 返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrModel.count;
}
// cell赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    MovieModel *model = _arrModel[indexPath.row];
    cell.nameLabel.text = model.title;
    NSString *str = model.image;
    [cell.img sd_setImageWithURL:[NSURL URLWithString:str]];
    
    return cell;
}
// 返回行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}


// 右item
- (void)itemAction:(UIBarButtonItem *)item {

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置每个item的大小
    flowLayout.itemSize = CGSizeMake(110, 150);
    // 设置每个item的最小列间距
    flowLayout.minimumInteritemSpacing = 10;
    // 设置每个item的最小行间距
    flowLayout.minimumLineSpacing = 50;
    
    //设置分区间隔
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    // 设置尾部尺寸
    flowLayout.footerReferenceSize = CGSizeMake(100, 150);
    // 初始化
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    // 设置代理人
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    // 注册
    [collectionView registerClass:[RootCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:collectionView];
    [collectionView release];
    
    UIImage *img = [UIImage imageNamed:@"btn_nav_list@2x.png"];
    img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(itemAction2:)];
    self.navigationItem.rightBarButtonItem = item2;
    
    [item2 release];
    
}
// collection 方法
- (void)itemAction2:(UIBarButtonItem *)item {
    UIImage *img5 = [UIImage imageNamed:@"btn_nav_collection@2x.png"];
    img5 = [img5 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithImage:img5 style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    
    self.navigationItem.rightBarButtonItem = item2;
    [item2 release];
    
    [self.view addSubview:_tableview];

    _tableview.delegate = self;
    _tableview.dataSource = self;
    
    [_tableview registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"cell"];

    
    
}
//返回item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _arrModel.count;
}
// 创建item视图对象
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RootCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
   MovieModel *model = _arrModel[indexPath.row];
    NSString *str = model.image;
    NSLog(@"%@", model.image);
    [cell.img sd_setImageWithURL:[NSURL URLWithString:str]];
    cell.label.text = model.title;
    
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
